package com.k2data.k2app.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.k2data.k2app.domain.deviceRest.SysLog;
import com.k2data.k2app.domain.redis.Login;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.rest.SystemClient;
import com.k2data.k2app.rest.domain.RectList;
import com.k2data.k2app.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author cuilibo@k2data.com.cn
 * @Date 17-12-27 下午2:57.
 */
@Api(description = "redis")
@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private SystemClient systemClient;

    @ApiOperation(value = "删除用户登陆缓存信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "login", value = "login", required = true, dataType = "Login", paramType = "body")
    })
    @PostMapping(value = "/logout")
    public ResponseEntity<CommonResultResponse> logout(@RequestBody Login login) throws Exception {
        redisService.logout(login);
        SysLog sysLog = new SysLog();
        sysLog.setModelName("k2alpha-health");
        sysLog.setClassName(this.getClass().getName());
        sysLog.setMethod(Thread.currentThread().getStackTrace()[1].getMethodName());
        sysLog.setCreateBy(login.getLoginName());
        sysLog.setCreateDate(new Date());
        sysLog.setType("logout");
        sysLog.setMessage("用户<"+login.getLoginName()+">退出");
        systemClient.insertLog(sysLog);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "用户登陆缓存信息")
    @GetMapping(value = "/loginInfo")
    public ResponseEntity<CommonResultResponse> logInfo() throws Exception {
        List<Object> lrange = redisService.lrange("users", 0, 1000);
        Map<String,Integer> infos = Maps.newHashMap();
        List<Map> list = Lists.newArrayList();
        lrange.forEach(s->{
            String[] a = s.toString().split("###");
            Integer count = infos.getOrDefault(a[0],0);
            infos.put(a[0],++count);
        });
        infos.forEach((k,v)->{
            Map map = Maps.newHashMap();
            map.put("name",k);
            map.put("count",v);
            list.add(map);
        });

        Map result = Maps.newHashMap();
        result.put("users",list);
        RectList userByLoginName = systemClient.getUserByLoginName(null);
        result.put("countAll",userByLoginName.getResult().size());
        Map<String,Map> users = Maps.newHashMap();
        users.put("users",infos);
        return ResponseHelper.success(result);
    }
}
